<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../../asserts/js/jquery-2.1.1.min.js"></script>
    <link rel="stylesheet" href="../plugins/Bootstrap/css/bootstrap.css">
    <script src="../plugins/Bootstrap/js/bootstrap.js"></script>

    <script>
        let tid=1
        $(function () {
            $.ajax({
                url:"/taobei_web/disc/getDiscBySessionId",
                dataType:'json',
                success:function (respBean) {
                    if (respBean.status == 500){
                        alert(respBean.msg)
                    } else {
                        let text ="";
                        for (let i = 0; i < respBean.data.length; i++) {
                            text+="<tr>";
                            text+="<td>"+respBean.data[i].id+"</td>"
                            text+="<td>"+respBean.data[i].name+"</td>"
                            text+="<td>"+respBean.data[i].prePrice+"</td>"
                            text+="<td>"+respBean.data[i].doorSill+"</td>"
                            if (respBean.data[i].tid==1){
                                text+="<td>店铺可用</td>"
                            } if (respBean.data[i].tid==2){
                                text+="<td>仅限特定商品可用</td>"
                            }
                            text+="<td>"+respBean.data[i].sid+"</td>"
                            text+="<td>"+respBean.data[i].pid+"</td>"
                            text+="<td>"+respBean.data[i].quantity+"</td>"
                            text+="<td>"+respBean.data[i].releaseTime+"</td>"
                            text+="<td>"+respBean.data[i].validity+"</td>"
                            text+="</tr>";

                        }
                        $("#tbody").html(text);
                    }
                }
            })
                $("#addDiscount").click(function () {
                    $.ajax({
                        url:'/taobei_web/disc/addDiscount',
                        type:'post',
                        data:{
                            name:$("#name").val(),
                            prePrice:$("#prePrice").val(),
                            doorSill:$("#doorSill").val(),
                            quantity:$("#quantity").val(),
                            validity:$("#validity").val(),
                            pid:$("#productId").val(),
                            tid:tid,
                        },
                        dataType:'json',
                        success:function (respBean) {
                            if (respBean.status==500){
                                alert(respBean.msg)
                            } else {
                                alert(respBean.msg)
                                history.go(0)
                            }
                        }
                    })
                })

            $("input[name='scope']").click(function () {
              if (document.getElementById("product").checked) {
                  tid=2
                  $.ajax({
                      url:'/taobei_web/Store/queryProductIdAndName',
                      dataType:'json',
                      success:function (respBean) {
                         if (respBean.status==500){
                             alert(respBean.msg);
                         } else {
                             let text = "";
                             for (let i = 0; i < respBean.data.length; i++) {
                                    text +="<option value='"+respBean.data[i].id+"'>"+respBean.data[i].name+"</option>"
                             }
                             $("#productId").html(text)
                         }
                      }
                  })
              }if (document.getElementById("store").checked){
                    $("#productId").html("")
                }
            })
        })
    </script>
</head>
<body>
<button type="button" class="btn btn-group-vertical" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo">添加优惠券</button>


<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="exampleModalLabel">添加优惠券</h4>
            </div>
            <div class="modal-body">
                <form>

                    <div class="form-group">
                        <label  class="control-label">优惠券名称</label><br>
                        <input type="text" class="form-control" id="name">
                    </div>
                    <div class="form-group">
                        <label  class="control-label">优惠价格:</label><br>
                        <input type="number" class="form-control" id="prePrice">
                    </div>
                    <div class="form-group">
                        <label  class="control-label">使用门槛:</label><br>
                        <input type="number" class="form-control" id="doorSill">
                    </div>
                    <div class="form-group">
                        <label  class="control-label">优惠券数量:</label><br>
                        <input type="number" class="form-control" id="quantity">
                    </div>
                    <div class="form-group">
                        <label  class="control-label">有效时间:</label><br>
                        <input type="number" class="form-control" id="validity">
                    </div>
                    <div class="form-group">
                        <label  class="control-label">使用范围:</label><br>
                        本店铺可用<input type="radio" id="store" name="scope">
                        仅限特定商品<input type="radio" id="product" name="scope">
                    </div>
                    <div class="form-group">
                        <label class="control-label" id="choseProduct"></label>
                        <select id="productId">
                        </select>
                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">返回</button>
                <button type="button" class="btn btn-primary" id="addDiscount">确认添加</button>
            </div>
        </div>
    </div>
</div>


<table class="table table-bordered">
    <thead>
        <tr>
            <th>编号</th>
            <th>优惠券名称</th>
            <th>优惠价格</th>
            <th>使用门槛</th>
            <th>优惠券类型</th>
            <th>店铺id</th>
            <th>商品id</th>
            <th>优惠券数量</th>
            <th>发放时间</th>
            <th>有效天数</th>
        </tr>
    </thead>
    <tbody id="tbody">

    </tbody>
</table>
</body>
</html>